<%@page import="java.util.HashMap"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ page errorPage="../jsp/index.jsp?site=error"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@ page errorPage="../jsp/index.jsp?site=error"%>
<%@page import="java.sql.SQLException"%>
<%@page import="java.util.ArrayList"%>



<jsp:useBean id="SHA"
	class="de.fhhof.vinf.ssp.studienarbeit11.beans.SHA" scope="session" />
<jsp:useBean id="Validate"
	class="de.fhhof.vinf.ssp.studienarbeit11.beans.Validate"
	scope="session" />
<jsp:useBean id="SaveStrings"
	class="de.fhhof.vinf.ssp.studienarbeit11.beans.SaveStrings"
	scope="session" />
<%
	/*
	 *Save params from register.jsp in Hashmap
	 */
	HashMap<String, String> hasMap = new HashMap<String, String>();
	hasMap.put("first_name", request.getParameter("first_name"));
	hasMap.put("last_name", request.getParameter("last_name"));
	hasMap.put("email", request.getParameter("email"));
	hasMap.put("password", SHA.getSHA(request.getParameter("password")));
	hasMap.put("password2",
			SHA.getSHA(request.getParameter("password2")));

	ArrayList<String> errors = new ArrayList<String>();

	/*
	 *See if input of register.jsp is valid
	 */

	if (!Validate.isStringWithoutNumbers(hasMap.get("first_name"))) {
		errors.add("Please enter valid first name");
		hasMap.put("frist_name", "");
	}
	if (!Validate.isStringWithoutNumbers(hasMap.get("last_name"))) {
		errors.add("Please enter valid last name");
		hasMap.put("last_name", "");
	}
	if (!Validate.isValidEmail(hasMap.get("email"))) {
		errors.add("Please enter valid e-mail");
		hasMap.put("email", "");
	} else {
		if (Validate.isInDb(hasMap.get("email"), "email")) {
			errors.add("E-mail is already in use");
			hasMap.put("email", "");
		}
	}
	if (!Validate.isAtLeastFiveCharacters(request
			.getParameter("password"))) {
		errors.add("Password has to be at least five characters long");
	} else {
		if (!hasMap.get("password").equals(hasMap.get("password2"))) {
			errors.add("Passwords are not the same");
		}
	}

	/*
	 *If no errors: write to db
	 */
	if (errors.size() == 0) {
		Statement stmt;
		ResultSet res;
		Class.forName("com.mysql.jdbc.Driver");
		String url = "jdbc:mysql://localhost:3306/studienarbeit";
		Connection con = DriverManager.getConnection(url, "admin",
				"cisco");
		stmt = con.createStatement();

		try {
			stmt.executeUpdate("insert into studienarbeit.users(first_name,last_name,email,password,is_admin)"
					+ " values ('"
					+ hasMap.get("first_name")
					+ "','"
					+ hasMap.get("last_name")
					+ "','"
					+ hasMap.get("email")
					+ "','"
					+ hasMap.get("password") + "',0);");
		} catch (Exception e) {
			con.close();
			throw new SQLException(e);
		} finally {
			con.close();
		}
%>
<jsp:forward page="../jsp/index.jsp" />
<%
	/*
	 *If errors: right input is saved in HashMap, errors are saved in ArrayList
	 *both written to bean
	 */
	} else {
		SaveStrings.setArraylist(errors);
		SaveStrings.setHashMap(hasMap);
%>
<jsp:forward page="../jsp/index.jsp?site=register" />

<%
	}
%>